<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

/**
 * 定时执行的任务方法列表
 * 营销->商品促销 自动打上商品 促销 标签 karson 20171026
 * @auther shopex ecstore dev dev@shopex.cn
 * @version 0.1
 * @package ectools.lib.misc
 */
class b2c_statistics_autosalesgoodsref extends base_task_abstract implements base_interface_task{

	function exec($params=null) {
		logger::info('------进入定时执行的任务方法列表--------');

		$oGoodsPromotion = app::get("desktop")->model('tag_rel');
		//查找生效活动未打促销标签数据
		$sql="select  '2' as tag_id,goods_id  as rel_id,'b2c' as app_id,'goods' as tag_type ,unix_timestamp(now()) last_modify
		from sdb_b2c_goods_promotion_ref where unix_timestamp(now()) BETWEEN from_time and to_time and status =true
		and goods_id not in (select rel_id from sdb_desktop_tag_rel where tag_id =2 );";

		$promotionrefdata = $oGoodsPromotion->db->select($sql);
		if($promotionrefdata){
			try {
				foreach ($promotionrefdata as $key => $value) {
                              //sdb_desktop_tag_rel tag和对象关联表
                              //tag_id rel_id 联合主键  不允许重复插入
					$repeat ="select tag_id,rel_id from sdb_desktop_tag_rel where tag_id =2 and rel_id=".$value['rel_id'];
					if(!$oGoodsPromotion->db->select($repeat))
					{
						$res =  $oGoodsPromotion->insert($value);
					}
				}
			}
			catch (Exception $e) {
				$this->end(true, app::get('b2c')->_($e->getMessage()));
			}
		}

        //订单促销
		// $mOrderPromotion = $this->app->model("sales_rule_order");
		$sql="select * from  sdb_b2c_sales_rule_order where unix_timestamp(now()) BETWEEN from_time and to_time and status =true;";

		$promotionrefdata = $oGoodsPromotion->db->select($sql);
		if($promotionrefdata){
			foreach ($promotionrefdata as $key => $value) {

				$aResult = unserialize($value['conditions']);
               // var_dump($aResult['conditions'][0]['conditions'][0]['value']);
				if($aResult['conditions'][0]['conditions'][0]['value'])
				{
					$sale_arr = array();
					$sale_arr[] =array(
						'tag_id' => '2',
						'rel_id' => $aResult['conditions'][0]['conditions'][0]['value'],
						'app_id' => 'b2c',
						'tag_type' => 'goods',
						'last_modify' => time(),
						);
                   //sdb_desktop_tag_rel tag和对象关联表
                    //tag_id rel_id 联合主键  不允许重复插入
					$repeat ="select tag_id,rel_id from sdb_desktop_tag_rel where tag_id =2 and rel_id=". $aResult['conditions'][0]['conditions'][0]['value'];
					if(!$oGoodsPromotion->db->select($repeat))
					{
						$res =  $oGoodsPromotion->insert($sale_arr[0]);
					}

					

				}

			}

		}




	}

}


